public class HashBuck{
    class Node{
        public int key;
        public String value;
        public Node next;
        public Node(int key, String value){
            this.key = key;
            this.value = value;
        }
    }
    public Node[] array;
    public int usedSize = 0;
    public HashBuck(){
        array = new Node[10];
    }
    public void put(int key, String value){
        int index = key % array.length;
        //尾插法
        Node cur = array[index];
        //有没有这个key
        while(cur != null){
            if(cur.key == key){
                cur.value = value;
                return;
            }
            cur = cur.next;
        }
        Node node = new Node(key, value);
        node.next = array[index];
        array[index] = node;
        usedSize++;

        //数组容量超过负载因子，这里进行扩容
        if(loadFactor() > 0.75){
            resize();
        }
    }

    private void resize() {
        Node[] tmpArr = new Node[array.length * 2];
        for(int i = 0; i < array.length; i++){
            Node cur = array[i];
            //头插法
            while(cur != null){
                Node curNext = cur.next;
                int newIndex = cur.key % tmpArr.length;
                cur.next = tmpArr[newIndex];
                tmpArr[newIndex] = cur;
                cur = curNext;
            }
        }
        array = tmpArr;
    }

    private double loadFactor(){
        return usedSize * 1.0 / array.length;
    }

    public String get(int key){
        int index = key % array.length;
        Node cur = array[index];
        while(cur != null){
            if(cur.key == key){
                return cur.value;
            }
        }
        return null;
    }
}
